package data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import domain.Skin;

public class JDBCSkinDAO implements ISkinDAO{
	
	public List selectAllSkins(){
		
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		ResultSet result = null;
		List listaSkin = new LinkedList();
		String sql = "SELECT * FROM Skin ";
		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			while (result.next()) {
				Skin skin = rellenarSkin(result);
				listaSkin.add(skin);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return listaSkin;
	}

	public Skin select(String nombre){
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		ResultSet result = null;
		Skin skin = null;
		String sql = "SELECT * FROM Skin WHERE (NOMBRE = ?) ";
		try {	
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, nombre);
			result = stmt.executeQuery();
			while(result.next()){
				skin = rellenarSkin(result);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return skin;
	}
	
	private Skin rellenarSkin(ResultSet result) throws SQLException {
		Skin skin = new Skin();
		skin.setOid(result.getString("OID"));
		skin.setNombre(result.getString("NOMBRE"));
		skin.setDescripcion(result.getString("DESCRIPCION"));
		skin.setSkin(result.getString("SKIN"));
		return skin;
	}

}



